

enum MissionStatus {


    UNACCEPTABLE = 0,

    CAN_ACCEPT = 1,

    DURGING = 2,

    CAN_SUBMIT = 3,

    FINISHIED = 4
}

class Mission {

    id = 0;

    title = '';

    canAcceptContent: string = "";

    canSubmitContent: string = "";

    parseFromConfig(config: any) {
        this.id = config.id;
        this.title = config.title;
        this.canAcceptContent = config.canAcceptContent
        this.canSubmitContent = config.canSubmitContent;
        this.needLevel = config.needLevel;
        this.fromNpcId = config.fromNpcId;
        this.toNpcId = config.toNpcId;
        this.rewards = config.rewards;
    }



    needLevel = 0;

    fromNpcId = 0;

    toNpcId = 0;

    isAccepted = false;

    isSubmit = false;

    current = 0;

    total = 1;


    rewards: { type: string, num: number }[] = [];




    status: MissionStatus = 0;

    update() {
        let nextStatus: MissionStatus = 0;
        if (this.isSubmit) {
            nextStatus = MissionStatus.FINISHIED
        }
        else if (this.isAccepted) {
            if (this.current >= this.total) {
                nextStatus = MissionStatus.CAN_SUBMIT;
            }
            else {
                nextStatus = MissionStatus.DURGING;
            }

        }
        else {
            // if (this.needLevel > user.level) {
            //     nextStatus = MissionStatus.UNACCEPTABLE;
            // }
            // else {
            //     nextStatus = MissionStatus.CAN_ACCEPT;
            // }

        }


        if (nextStatus != this.status) {
            this.status = nextStatus;
            return true;
        }
        else {
            return false;
        }

    }
}


class MissionManager extends EventDispatcher {


    missions: Mission[] = [];

    constructor() {
        super();
    }

    acceptMission(mission: Mission) {
        mission.isAccepted = true;
        mission.current = 1;
        this.update();
    }

    parseFromConfig(config: any) {
        for (let item of config.tasks) {
            const mission1 = new Mission();
            mission1.parseFromConfig(item);
            missionManager.missions.push(mission1);
        }
    }

    submitMission(mission: Mission) {
        mission.isSubmit = true;

        this.update();
        const rewards = mission.rewards;
        // for (let reward of rewards) {
        //     if (reward.type == 'level') {
        //         user.level += reward.num;
        //     }
        //     else if (reward.type == 'sliverCoin') {
        //         user.sliverCoin += reward.num;
        //     }
        // }
    }

    init() {
        // user.addEventListener("userchange", () => {
        //     this.update();
        // });
        this.update();
    }

    update() {
        let needUpdate = false;
        for (let mission of this.missions) {
            const missionNeedUpdate = mission.update();
            if (missionNeedUpdate) {
                needUpdate = true;
            }
        }
        if (needUpdate) {
            this.dispatchEvent("missionUpdate", {});
        }
    }


}


const missionManager = new MissionManager();